package com.gen.utils.ngen;

import com.gen.utils.lan.jp.RomajiHenkan;
import org.yaml.snakeyaml.Yaml;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Random;

public class JpNameGenUtils {
    private static final String KANA_TO_ROMAJI_FILE = "japan_names.yml";
    private static  LinkedHashMap nameinfo;
    private static  List<List<String>> familyNames;
    private static  List<List<String>> femaleNames;
    private static  List<List<String>> maleNames;
    private static final String HIRAGANA = "hiragana";
    private static final String KATAKANA = "katakana";


    static {
        try{
            Yaml yaml = new Yaml();
            nameinfo = yaml.load(JpNameGenUtils.class.getClassLoader()
                    .getResourceAsStream("japan_names.yml"));
            familyNames = (List<List<String>>) nameinfo.get("last_name");
            femaleNames = (List<List<String>>) ((LinkedHashMap) nameinfo.get("first_name")).get("female");
            maleNames = (List<List<String>>) ((LinkedHashMap) nameinfo.get("first_name")).get("male");
        }catch (Exception e){
            e.printStackTrace();
        }


    }
    public static String genFamilyName(String type){
        if(HIRAGANA.equals(type)){
            return familyNames.get(new Random().nextInt(familyNames.size())).get(1);
        }else if(KATAKANA.equals(type)){
            return familyNames.get(new Random().nextInt(familyNames.size())).get(2);
        }else {
            return familyNames.get(new Random().nextInt(familyNames.size())).get(0);
        }
    }

    public static String genName(String type,int sex){
        if(HIRAGANA.equals(type)){
            if(sex ==0 ){
                return femaleNames.get(new Random().nextInt(femaleNames.size())).get(1);
            }else {
                return maleNames.get(new Random().nextInt(maleNames.size())).get(1);
            }
        }else if(KATAKANA.equals(type)){
            if(sex ==0 ){
                return femaleNames.get(new Random().nextInt(femaleNames.size())).get(2);
            }else {
                return maleNames.get(new Random().nextInt(maleNames.size())).get(2);
            }
        }else {
            if(sex ==0 ){
                return femaleNames.get(new Random().nextInt(femaleNames.size())).get(0);
            }else {
                return maleNames.get(new Random().nextInt(maleNames.size())).get(0);
            }
        }
    }
    public static String getInsNickname(int sex){
        String nicename = "";
        int r = new Random().nextInt(100);
        if(r<5){
            String fimalyName = JpNameGenUtils.genFamilyName(null);
            String name = JpNameGenUtils.genName(new Random().nextInt(100)<50?HIRAGANA:"",sex) ;
            String qian = fimalyName +(new Random().nextInt(100)<50?" ":"") + name;
            RomajiHenkan romajiHenkan = new RomajiHenkan();
            fimalyName = romajiHenkan.convert(fimalyName,false).replaceAll(" ","");
            name = romajiHenkan.convert(name,false).replaceAll(" ","");
            fimalyName = fimalyName.substring(0,1).toUpperCase()+fimalyName.substring(1);
            name = name.substring(0,1).toUpperCase()+name.substring(1);
            nicename = qian +" "+ fimalyName+" "+name;

        } else if(r<15){ // さとう ちなみ
            nicename = JpNameGenUtils.genFamilyName(HIRAGANA) + " "+ JpNameGenUtils.genName(HIRAGANA,sex) ;
        }else if(r<30){// ちなみ
            nicename = JpNameGenUtils.genName(JpNameGenUtils.HIRAGANA,sex);
        }else if(r<45){// アスカ
            nicename = JpNameGenUtils.genName(JpNameGenUtils.KATAKANA,sex);
        }else if(r<60){// 全民名字罗马化

            String fimalyName = JpNameGenUtils.genFamilyName(KATAKANA);
            String name = JpNameGenUtils.genName(JpNameGenUtils.KATAKANA,sex) ;
            RomajiHenkan romajiHenkan = new RomajiHenkan();
            fimalyName = romajiHenkan.convert(fimalyName,false).replaceAll(" ","");
            name = romajiHenkan.convert(name,false).replaceAll(" ","");
            fimalyName = fimalyName.substring(0,1).toUpperCase()+fimalyName.substring(1);
            name = name.substring(0,1).toUpperCase()+name.substring(1);
            nicename = fimalyName+" "+name;
         }else if(r<70){// 名字罗马化

            nicename = JpNameGenUtils.genName(JpNameGenUtils.KATAKANA,sex).replaceAll(" ","") ;
            RomajiHenkan romajiHenkan = new RomajiHenkan();
            nicename = romajiHenkan.convert(nicename,false).replaceAll(" ","");
            nicename = nicename.substring(0,1).toUpperCase()+nicename.substring(1);
        }else { // 藤村 みあさ 藤村みあさ 藤村流佳  藤村 流佳
            nicename = JpNameGenUtils.genFullName(sex,new Random().nextInt(100)<50?" ":"") ;
         }

        String num = "";
        if(new Random().nextInt(100)<5){
                int ageRand = new Random().nextInt(100);
                if(ageRand<100){
                    num = (1988+new Random().nextInt(15))+"";
                }
        }
        nicename = nicename + num;


        if(new Random().nextInt(100)<5){
            nicename = nicename + DynamicText.getRandEmoji();
        }
        if(new Random().nextInt(100)<10){
            nicename = NameGenUtils.toRandomFont(nicename);
        }
        return  nicename.trim();
    }

    public static String getGmailNickName(int sex){
        String nicename = "";
        int r = new Random().nextInt(100);
        if(r<50){ // さとう ちなみ
            nicename = JpNameGenUtils.genFamilyName(HIRAGANA) + " "+ JpNameGenUtils.genName(HIRAGANA,sex) ;
        }else { // 藤村 みあさ 藤村みあさ 藤村流佳  藤村 流佳
            nicename = JpNameGenUtils.genFullName(sex,new Random().nextInt(100)<50?" ":" ") ;
        }
        return  nicename.trim();
    }
    public static String getFBNickName(int sex){
        return getFBNickName(sex,false);
    }

    public static String getFBNickName(int sex,boolean useEn){
        int r = new Random().nextInt(100);
        String familyName = genFamilyName(null) ;

        if(r<50 || useEn){
            RomajiHenkan romajiHenkan = new RomajiHenkan();
            familyName =  romajiHenkan.convert(familyName,false) ;
            String name = genName(KATAKANA,sex);//部分字符无法转，取可以转的
            name = romajiHenkan.convert(name,false) ;
            int namelen = name.split(" ").length;
            if(namelen>=2){
                if(name.split(" ")[0].length()>1){
                    name = name.split(" ")[0];
                }else {
                    name = name.replaceAll(" ","");
                }
            }

            r = new Random().nextInt(100);
            if(r < 50){
                return name +" "+familyName ;
            }else {
                return familyName + " " +name;

            }
        }else {
            String name = genName("",sex);
            return  familyName + " " +name;
        }
     }

    public static String genFullName(int sex){
        return genFullName(sex," ");
    }
    public static String genFullName(int sex,String split){
        String type = "";
        int rate = new Random().nextInt(100);
        if(rate<50){
            type = HIRAGANA;
        }
        return genFamilyName(null) + split +genName(type,sex);
    }
    public static void main(String[] arg ){



//        Yaml yaml = new Yaml();
//        Object ret = yaml.load(JPNameGenUtils.class.getClassLoader()
//                .getResourceAsStream("japan_names.yml"));
                 RomajiHenkan romajiHenkan = new RomajiHenkan();
////                 ;
                 System.out.println(romajiHenkan.convert("ゅり",true));
         for (int i = 0;i<1000;i++ ) {
//            System.out.println(getGmailNickName(0));

            String name = getFBNickName(0,true);
             System.out.println(name);

//             String cname = romajiHenkan.convert(name,true);

//             System.out.println(name+" "+cname);
        }

    }

}
